package org.springframework.security.web.authentication.rememberme;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.exception.GlobalErrorCode;
import com.nuanshui.heatedloan.controller.ResponseObject;

/**
 * 
 * @ClassName: AuthenticationSuccessHandlerImpl
 * @author guohao
 * @date 2015年4月15日 下午2:52:40
 *
 */
public class AuthenticationFailureHandlerImpl implements
		AuthenticationFailureHandler {

	private Logger log = LoggerFactory.getLogger(getClass());
	@Override
	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException exception)
			throws IOException, ServletException {
		ResponseObject<Boolean> object = new ResponseObject<Boolean>(GlobalErrorCode.UNKNOWN);
		object.setData(false);
		if(exception instanceof UsernameNotFoundException) {
			object.setMoreInfo("该手机号未注册");
		}else if(exception instanceof BadCredentialsException)  {
		   object.setMoreInfo("密码错误");
		}else {
			object.setMoreInfo("服务器繁忙请稍后再试！");
			log.error("登录服务器繁忙",exception);
		}
		
		response.getWriter().println(JSON.toJSON(object));		
	}

	
}